﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace TMI85303.UI.UserControls
{
    public interface IDrawable
    {
        void DrawLine(double x1, double y1, double x2, double y2);
        void DrawLine(double x1, double y1, double x2, double y2, Brush brush);
        void DrawRectangle(double x, double y, double width, double height);
        void DrawRectangle(double x, double y, double width, double height, Brush brush);
        void DrawTriangle(double x1, double y1, double x2, double y2, double x3, double y3);
        void DrawPoint(double x, double y, Brush color, double size = 1.0);
        void DrawPoint(double x, double y, double size = 1.0);
        void DrawCircle(double x, double y, double diameter,double strokeThickness, Brush brush);
        void DrawCircle(double x, double y, double diameter,double strokeThickness);
    }

    public class CustomCanvas : Canvas, IDrawable
    {
        public void DrawCircle(double x, double y, double diameter, double strokeThickness, Brush brush)
        {
            Ellipse point = new()
            {
                Width = diameter,
                Height = diameter,
                Fill = null, // 不填充
                Stroke = brush, // 设置轮廓颜色
                StrokeThickness = strokeThickness // 设置轮廓宽度
            };
            SetLeft(point, x - diameter / 2);
            SetTop(point, y - diameter / 2);
            Children.Add(point);
        }

        public void DrawCircle(double x, double y, double diameter, double strokeThickness) => DrawCircle(x,y,diameter,strokeThickness, Brushes.Black);

        public void DrawLine(double x1, double y1, double x2, double y2) => DrawLine(x1, y1, x2, y2, Brushes.Black);
        //
        public void DrawLine(double x1, double y1, double x2, double y2,Brush brush)
        {
            Line line = new()
            {
                X1 = x1,
                Y1 = y1,
                X2 = x2,
                Y2 = y2,
                Stroke = brush
            };
            Children.Add(line);
        }

        public void DrawPoint(double x, double y,Brush color, double size = 1.0)
        { 
            Ellipse point = new()
            {
                Width = size,
                Height = size,
                Fill = color
            };
            // 设置点的位置，注意Ellipse的位置是根据左上角定位的，因此需要根据点的大小进行调整
            SetLeft(point, x - size / 2);
            SetTop(point, y - size / 2);
            Children.Add(point);
        }

        public void DrawPoint(double x, double y, double size = 1) => DrawPoint(x,y, Brushes.Black, size);

        public void DrawRectangle(double x, double y, double width, double height) => DrawRectangle(x, y, width, height, Brushes.Black);
        public void DrawRectangle(double x, double y, double width, double height, Brush brush)
        {
            Rectangle rect = new()
            {
                Width = width,
                Height = height,
                Stroke = brush
            };
            SetLeft(rect, x);
            SetTop(rect, y);
            Children.Add(rect);
        }

        public void DrawTriangle(double x1, double y1, double x2, double y2, double x3, double y3)
        {
            Polygon triangle = new()
            {
                Stroke = Brushes.Black, // 边框颜色
                Fill = Brushes.LightBlue, // 填充颜色，可根据需要调整
                StrokeThickness = 2, // 边框厚度
                                     // 设置三角形的顶点
                Points = new PointCollection{ new(x1, y1),new(x2, y2),new(x3, y3)}
            };

            this.Children.Add(triangle);
        }
    }

}
